<template>
  <div>
    <!--     升级规则配置 -->
    <el-row style="text-align: right">
      <el-button type="primary" @click="addUpdataRules" v-show="panePermiss[142]">{{ $t('新增') }}</el-button>
    </el-row>
    <el-row style="margin-top: 10px">
      <Table :page="page" row-key="id" :table-data="tableData" :table-column="tableColumn"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <template slot="operate">
          <el-table-column :label="$t('操作')" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="editProduct(scope.row)" v-show="panePermiss[144]">{{
                  $t('编辑')
                }}
              </el-button>
              <el-button type="text" @click="deleteProduct(scope.row)" v-show="panePermiss[1057]">{{
                  $t('删除')
                }}
              </el-button>
            </template>
          </el-table-column>
        </template>
      </Table>
    </el-row>
    <Dialog @cancel="visible.isShow = false" @confirm="visibleConfirm"
            :visible.sync="visible.isShow" :title="visible.title" @close="clearForm" :confirm-loading="confirmLoading">
      <el-form :model="form" ref="form" label-width="150px" :rules="rulesForm">
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item :label="$t('升级规则名称')" prop="rulesName">
              <el-input v-model="form.rulesName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('正常结清次数')" prop="rulesNum">
              <el-input v-model.number="form.rulesNum" oninput="value = value.replace(/[^\d]/g, '')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('提升金额')" prop="amount">
              <el-input v-model="form.amount" oninput="value = value.replace(/[^\d.]/g, '')"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('描述')" prop="describe">
              <el-input type="textarea" v-model="form.describe"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </Dialog>
  </div>
</template>

<script>
import {deepClone} from "@/utils/tools";
import {
  userLoanRulesList,
  userLoanRulesDelete,
  addUserLoanRules,
  updateUserLoanRules,
  updateUserLoanRulesStatus
} from '@/api/product'

export default {
  props: ['panePermiss'],
  data() {
    const validator = (rule, value, callBack) => {
      if (parseFloat(value) === 0) {
        return callBack(new Error(this.$t('不能为零')))
      }
      return callBack()
    }
    return {
      tableData: [],
      tableColumn: [
        {label: this.$t('升级规则名称'), prop: 'rulesName', align: 'center',},
        {label: this.$t('正常结清次数'), prop: 'rulesNum', align: 'center',},
        {label: this.$t('提升金额'), prop: 'amount', align: 'center',},
        {label: this.$t('创建时间'), prop: 'createTime', align: 'center', width: 150},
        {label: this.$t('更新时间'), prop: 'updateTime', align: 'center', width: 150},
      ],
      form: {},
      rulesForm: {
        rulesName: [
          {required: true, message: this.$t('不能为空')},
        ],
        rulesNum: [
          {required: true, message: this.$t('不能为空')},
          {type: 'number', message: this.$t('非数字')},
          {required: true, validator: validator}
        ],
        amount: [
          {required: true, message: this.$t('不能为空')},
          {required: true, validator: validator}
        ],
      },
      visible: {
        isShow: false,
        type: '',
        title: '',
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      loanAmountSelect: [],
      confirmLoading: false
    }
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const params = {
        ...this.page,
        type: 1,
        productId: this.$route.query.productId,
        rulesType: 1
      }
      const res = await userLoanRulesList(params)
      const {list, total} = res.data
      if (res.code === 1) {
        this.tableData = list.map(item => {
          item.status = item.status === 1
          return item
        })
        this.page = Object.assign({}, this.page, {
          total: total
        })
      }
    },
    clearForm() {
      this.form = {}
      this.$refs.form.resetFields()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    async changeStatus(row) {
      const res = await updateUserLoanRulesStatus({
        id: row.id,
        status: row.status ? 2 : 1,
        productId: this.$route.query.productId,
      })
      if (res.code === 1) {
        this.$message.success(this.$t('修改成功'))
        this.getTableData()
      }
    },
    async addUpdataRules() {
      this.visible = {
        isShow: true,
        type: 'add',
        title: this.$t('添加升级规则')
      }
    },
    async editProduct(row) {
      this.form = deepClone(row)
      this.visible = {
        isShow: true,
        type: 'edit',
        title: this.$t('编辑升级规则')
      }
    },
    async deleteProduct({id}) {
      this.$confirm(this.$t('确定删除?'), this.$t('提示'), {
        type: 'warning'
      }).then(async () => {
        const res = await userLoanRulesDelete({id})
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: this.$t('删除成功')
          });
          this.getTableData()
        }
      }).catch(() => {
      });
    },
    async visibleConfirm() {
      this.$refs.form.validate(async v => {
        if (v) {
          let message = ''
          let api
          this.confirmLoading = true
          const params = {
            ...this.form,
            amount: parseFloat(this.form.amount),
            productId: this.$route.query.productId,
            type: 1,
            rulesType: 1,
          }
          if (this.visible.type === 'add') {
            api = addUserLoanRules(params)
            message = this.$t('新增成功')
          } else {
            api = updateUserLoanRules(params)
            message = this.$t('修改成功')
          }
          const res = await api
          if (res.code === 1) {
            this.$message.success(message)
            this.confirmLoading = false
            this.clearForm()
            this.visible.isShow = false
            this.getTableData()
          } else {
            this.confirmLoading = false
          }
        }
      })

    }
  },
}
</script>

<style scoped>

</style>
